<template>
  <div>
    <!-- 列表 -->
    <ul class="TemplateStyle">
      <!-- 领用日期 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/date.png" />
          <span class="li_txt">领用日期</span>
        </div>
        <div class="label">
          <input type="text" :value="userDate" :disabled="true" />
        </div>
      </li>
      <!-- 爱心码 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/loveCode.png" />
          <span class="li_txt">爱心码</span>
        </div>
        <div class="label">
          <input type="number" @blur="Mouse_out" v-model="donateId" />
        </div>
      </li>
      <!-- 领用物资(名称) -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/birth.png" />
          <span class="li_txt">领用物资(名称)</span>
        </div>
        <div class="label">
          <input type="text" :value="useName" :disabled="true" />
        </div>
      </li>
      <!-- 领用物资(单位) -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/order.png" />
          <span class="li_txt">领用物资(单位)</span>
        </div>
        <div class="label">
          <input type="text" :value="useUnit" :disabled="true" />
        </div>
      </li>
      <!-- 领用人 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/recipient.png" />
          <span class="li_txt">领用人</span>
        </div>
        <div class="label">
          <input type="text" v-model="usePerson" />
        </div>
      </li>
      <!-- 附件 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/Enclosure.png" />
          <span class="li_txt">附件</span>
        </div>
        <van-uploader accept="image/png, image/jpeg, image/jpg" :after-read="afterRead">
          <van-button
            style="border-radius:.8rem;height:2.1rem;line-height:2.1rem"
            icon="photo"
            type="primary"
          >上传领用原始凭证</van-button>
        </van-uploader>
      </li>

      <!-- 确认按钮 -->
      <button class="btn" @click="btnReceive">确定</button>
    </ul>
    <!-- loading -->
    <div style="padding-bottom: 4rem; position:top:50%">
      <van-loading
        v-show="loading"
        style="position: absolute;top: 50%;left: 46%;"
        type="spinner"
        color="#1989fa"
      ></van-loading>
    </div>
  </div>
</template>
<script>
import moment from "moment";

export default {
  name: "ReceiveMaterial",
  data() {
    return {
      userDate: "", // 展示给用户
      useTime: "",
      donateId: "",
      useName: "",
      useUnit: "",
      usePerson: "",
      img: "",
      loading: false // loading
    };
  },
  created() {
    // 自动生成日期
    let nowDate = new Date();
    this.useTime = moment(nowDate).format("YYYY-MM-DD HH:mm:ss");
    this.userDate = moment(nowDate).format("YYYY-MM-DD");
  },
  methods: {
    // 根据爱心码自动填写内容
    Mouse_out() {
      if (!this.donateId) {
        this.useName = "";
        this.useUnit = "";
      } else if (this.donateId != null) {
        let loveCode = {
          userName: JSON.parse(localStorage.getItem("login")).res.user.userName,
          token: JSON.parse(localStorage.getItem("login")).res.token,
          donateId: this.donateId
        };
        //通过爱心码调用物资捐赠者信息;
        this.$api
          .getDonateProductById(loveCode)
          .then(res => {
            this.useName = res[0].donateName;
            this.useUnit = res[0].donateUnit;
          })
          .catch(res => {
            this.$dialog.alert({
              message: "未查询到该爱心码"
            });
            this.useName = "";
            this.useUnit = "";
          });
      }
    },
    // 附件图片
    afterRead(file) {
      this.img = file.content;
    },
    btnReceive() {
      if (!this.donateId) {
        this.$dialog.alert({
          message: "请输入爱心码"
        });
      } else {
        this.loading = true; // loading显示
        let params = {
          token: JSON.parse(localStorage.getItem("login")).res.token,
          userName: JSON.parse(localStorage.getItem("login")).res.user.userName,
          useTime: this.useTime,
          donateId: this.donateId,
          useName: this.useName,
          useUnit: this.useUnit,
          usePerson: this.usePerson,
          attachment: this.img
        };
        this.$api
          .requestuseProduct(params)
          .then(res => {
            this.loading = false; // loading隐藏
            this.$dialog.alert({
              message: res
            });
          })
          .catch(err => {
            this.loading = false; // loading隐藏
            this.$dialog.alert({
              message: err
            });
          });
      }
    }
  }
};
</script>
<style>
@import "../../assets/css/TemplateStyle.css";
</style>
